﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CP.Utility
{
    public static class AttachedProperty
    {
        private static Dictionary<object, Dictionary<string, object>> _table =
             new Dictionary<object, Dictionary<string, object>>();

        public static T GetAttachedPropertyValue<T>(this object owner, string propertyName)
        {
            Dictionary<string, object> values;
            if (_table.TryGetValue(owner, out values))
            {
                object temp;
                if (values.TryGetValue(propertyName, out temp))
                {
                    return (T)temp;
                }
            }

            return default(T);
        }

        public static void SetAttachedPropertyValue<T>(this object owner, string propertyName, T value)
        {
            Dictionary<string, object> values;
            if (!_table.TryGetValue(owner, out values))
            {
                values = new Dictionary<string, object>();
                _table.Add(owner, values);
            }

            values[propertyName] = value;
        }
    } 
}
